package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-06-26
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService apUserService;

    @Autowired
    private MediaFeign mediaFeign;

    @Autowired
    private ArticleFeign articleFeign;

    @Override
    public ResponseResult listByStatus(AuthDto dto) {

        //分页查询
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();

        if (dto.getStatus() != null) {
            query.eq(ApUserRealname::getStatus, dto.getStatus());
        }

        IPage<ApUserRealname> page1 = this.page(page, query);

        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),
                page1.getTotal(), page1.getRecords());


        return result;
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {
        //0驳回  1通过
        ApUserRealname userRealname = this.getById(dto.getId());
        if (type == 0) {
            //0驳回 需要修改实名认证的状态和原因
            userRealname.setStatus(2);
            userRealname.setReason(dto.getMsg());
            this.updateById(userRealname);
        } else {
            //1通过]
            //查询app用户
            ApUser apUser = apUserService.getById(userRealname.getUserId());
            //成为自媒体人
            WmUser wmUser = saveWmUser(apUser);
            //成为文章作者
            ApAuthor apAuthor = saveApAuthor(apUser, wmUser);

            //WmUser中给ApAuthorId赋值
            wmUser.setApAuthorId(apAuthor.getId());
            mediaFeign.updateWmUser(wmUser);

            //修改userRealname状态
            userRealname.setStatus(9);
            this.updateById(userRealname);
            //修改ApUser状态
            apUser.setIsIdentityAuthentication(true);
            apUser.setFlag(1);//0 普通用户     1 自媒体人     2 大V
            apUserService.updateById(apUser);
        }

        return ResponseResult.okResult();
    }

    private ApAuthor saveApAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
//        apAuthor.setId();//主键自增
        apAuthor.setName(apAuthor.getName());
        apAuthor.setType(2);//0 爬取数据    1 签约合作商     2 平台自媒体人
        apAuthor.setUserId(apAuthor.getId());
        apAuthor.setWmUserId(wmUser.getId());

        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if (responseResult.getCode() == 0) {
            return responseResult.getData();
        } else {
            throw new RuntimeException("远程调用失败");
        }
    }

    private WmUser saveWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
//        wmUser.setId();//主键自增
        wmUser.setApUserId(apUser.getId());
//        wmUser.setApAuthorId();//TODO文正作者id 无法赋值
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
//        wmUser.setNickname();//无法赋值
        wmUser.setImage(apUser.getImage());
//        wmUser.setLocation()//无法赋值;
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9);//9 正常使用
//        wmUser.setEmail();//无法赋值
        wmUser.setType(0);//账号类型   0 个人    1 企业    2 子账号
        wmUser.setScore(0);//运营评分
//        wmUser.setLoginTime();//无法赋值


        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        if (responseResult.getCode() == 0) {
            return responseResult.getData();
        } else {
            throw new RuntimeException("远程调用失败");
        }

    }


}
